<template>
  <div class="app-container">
    <!--<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="数据状态，详细参考c_status表，S 保存，0, 在用 1失效" prop="statusCd">
        <el-input
          v-model="queryParams.statusCd"
          placeholder="请输入数据状态，详细参考c_status表，S 保存，0, 在用 1失效"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="machineId">
        <el-input
          v-model="queryParams.machineId"
          placeholder="请输入设备ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区ID" prop="communityId">
        <el-input
          v-model="queryParams.communityId"
          placeholder="请输入小区ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="外部人脸ID" prop="extUserId">
        <el-input
          v-model="queryParams.extUserId"
          placeholder="请输入外部人脸ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门禁卡ID" prop="cardId">
        <el-input
          v-model="queryParams.cardId"
          placeholder="请输入门禁卡ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门禁卡号" prop="cardNumber">
        <el-input
          v-model="queryParams.cardNumber"
          placeholder="请输入门禁卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->
      <div class="filter-container" style="margin-bottom:10px">
          <el-input
                  v-model="queryParams.machineName"
                  placeholder="请输入门禁名称"
                  style="width: 200px;"
                  class="filter-item"
          />
          <el-input
                  v-model="queryParams.name"
                  placeholder="请输入用户名称"
                  style="width: 200px;"
                  class="filter-item"
          />

          <el-input
                  v-model="queryParams.machineCode"
                  placeholder="请输入门禁编码"
                  style="width: 200px;"
                  class="filter-item"
          />

          <el-button
                  v-waves
                  class="filter-item"
                  type="primary"
                  icon="el-icon-search"
                  @click="getAccessControlFace"
          >查询</el-button>
      </div>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['access:face:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['access:face:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['access:face:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['access:face:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--<el-table v-loading="loading" :data="faceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名称" align="center" prop="name" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人脸http 地址" align="center" prop="facePath" />
      <el-table-column label="数据状态，详细参考c_status表，S 保存，0, 在用 1失效" align="center" prop="statusCd" />
      <el-table-column label="身份证" align="center" prop="idNumber" />
      <el-table-column label="设备ID" align="center" prop="machineId" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="消息" align="center" prop="message" />
      <el-table-column label="小区ID" align="center" prop="communityId" />
      <el-table-column label="外部人脸ID" align="center" prop="extUserId" />
      <el-table-column label="门禁卡ID" align="center" prop="cardId" />
      <el-table-column label="门禁卡号" align="center" prop="cardNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['access:face:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['access:face:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>-->

      <el-table
              v-loading="loading"
              :data="faceList"
              element-loading-text="Loading"
              border
              fit
              highlight-current-row
      >
          <el-table-column align="center" label="编号" width="90">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
          </el-table-column>
          <el-table-column align="center" label="人脸">
              <template slot-scope="scope">
                  <el-image
                          ref="lazyImg"
                          lazy
                          class="vx-lazyLoad"
                          :src="scope.row.facePath"
                          :fit="fit"
                          :preview-src-list="[scope.row.facePath]"
                  >
                      <div slot="placeholder" class="image-slot">
                          <i class="el-icon-loading"></i>加载中
                      </div>
                      <div slot="error" class="image-slot">
                          <i class="el-icon-picture-outline"></i>
                      </div>
                  </el-image>
              </template>
          </el-table-column>
          <el-table-column align="center" label="用户名称">
              <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>
          <el-table-column align="center" label="身份证">
              <template slot-scope="scope">{{ scope.row.idNumber }}</template>
          </el-table-column>
          <el-table-column align="center" label="门禁名称">
              <template slot-scope="scope">{{ scope.row.machineName }}</template>
          </el-table-column>
          <el-table-column align="center" label="状态">
              <template slot-scope="scope">{{ getStateName(scope.row.state) }}</template>
          </el-table-column>
          <el-table-column align="center" label="描述">
              <template slot-scope="scope">{{ scope.row.message }}</template>
          </el-table-column>
          <el-table-column label="门禁编码" align="center">
              <template slot-scope="scope">
                  <span>{{ scope.row.machineCode }}</span>
              </template>
          </el-table-column>
          <el-table-column label="门禁IP" align="center">
              <template slot-scope="scope">
                  <span>{{ scope.row.machineIp }}</span>
              </template>
          </el-table-column>
          <el-table-column class-name="status-col" label="创建时间" align="center">
              <template slot-scope="scope">{{ scope.row.createTime }}</template>
          </el-table-column>
      </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人脸对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="主键ID" prop="id">
          <el-input v-model="form.id" placeholder="请输入主键ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="人脸http 地址" prop="facePath">
          <el-input v-model="form.facePath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="数据状态，详细参考c_status表，S 保存，0, 在用 1失效" prop="statusCd">
          <el-input v-model="form.statusCd" placeholder="请输入数据状态，详细参考c_status表，S 保存，0, 在用 1失效" />
        </el-form-item>
        <el-form-item label="身份证" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="设备ID" prop="machineId">
          <el-input v-model="form.machineId" placeholder="请输入设备ID" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="消息" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="小区ID" prop="communityId">
          <el-input v-model="form.communityId" placeholder="请输入小区ID" />
        </el-form-item>
        <el-form-item label="外部人脸ID" prop="extUserId">
          <el-input v-model="form.extUserId" placeholder="请输入外部人脸ID" />
        </el-form-item>
        <el-form-item label="门禁卡ID" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入门禁卡ID" />
        </el-form-item>
        <el-form-item label="门禁卡号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入门禁卡号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFace, getFace, delFace, addFace, updateFace, exportFace } from "@/api/access/face";

export default {
  name: "Face",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人脸表格数据
      faceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        startTime: null,
        endTime: null,
        facePath: null,
        statusCd: null,
        idNumber: null,
        machineId: null,
        state: null,
        message: null,
        communityId: null,
        extUserId: null,
        cardId: null,
        cardNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        facePath: [
          { required: true, message: "人脸http 地址不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        statusCd: [
          { required: true, message: "数据状态，详细参考c_status表，S 保存，0, 在用 1失效不能为空", trigger: "blur" }
        ],
        machineId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询人脸列表 */
    getList() {
      this.loading = true;
      listFace(this.queryParams).then(response => {
        this.faceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        name: null,
        startTime: null,
        endTime: null,
        facePath: null,
        createTime: null,
        statusCd: null,
        idNumber: null,
        machineId: null,
        state: null,
        message: null,
        communityId: null,
        extUserId: null,
        cardId: null,
        cardNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加人脸";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFace(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改人脸";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFace(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFace(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除人脸编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFace(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有人脸数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFace(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
